// pages/pallet_details/pallet_details.js
var app = getApp();
var util = require('../../util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 选择的图片装货
    selectImg: [],

    //最终上传的图片
    imgs: [],
    // 选择的图片卸货
    select: [],

    //最终上传的图片
    img: [],
    loading:true
  },
  /**
 * 生命周期函数--监听页面加载
 */
  onLoad: function (options) {
    var that = this;
    that.setData({
      options: options
    })
    that.position();
    app.getLoginSession(function (session) {
      wx.request({
        url: app.api + '/wx_log_test/order/' + options.id,
        data:{
          token:session
        },
        success:function(res){
          var res = res.data;
          if(res.msg=="OK"){
            var data = res.data;
            that.setData({
              data: that.deallist(data),
              loading:false
            })
            console.log(parseInt(that.data.data.cargo.sendDate2) / 1000 + 691200);
            that.data.data.order.sendstop = util.formatDate(new Date((parseInt(that.data.data.cargo.sendDate2) / 1000 + 691200)*1000), 'YYYY-MM-dd hh:mm:ss');
            that.data.data.order.deliverysend = util.formatDate(new Date((parseInt(that.data.data.order.loadOn) / 1000 + 691200)*1000), 'YYYY-MM-dd hh:mm:ss');
            that.data.data.cargo.sendDate1 = util.formatDate(new Date(that.data.data.cargo.sendDate1), 'YYYY-MM-dd hh:mm:ss');
            that.data.data.cargo.sendDate2 = util.formatDate(new Date(that.data.data.cargo.sendDate2), 'YYYY-MM-dd hh:mm:ss');
            that.data.data.order.deliveryOn = util.formatDate(new Date(that.data.data.order.deliveryOn), 'YYYY-MM-dd hh:mm:ss');
            that.data.data.order.unloadOn = util.formatDate(new Date(that.data.data.order.unloadOn), 'YYYY-MM-dd hh:mm:ss');
            that.setData({
              data:that.data.data
            })
          }else{
            that.setData({
              errorTips:'接口连接错误',
              loading:true
            })
          }
        },
        fail:function(res){
          that.setData({
            errorTips: '接口连接错误',
            loading: true
          })
        }
      })
    })
  },
  //更改状态
  deallist(list) {
    if (list.status == 7 && list.order.loadWeight != 0 && list.order.unloadWeight == 0) {
      list.status = 5;//运输中-->已送达
    } else if (list.status == 7 && list.order.loadWeight ==0){
      list.status = 3;//待提货-->提货完成
    } else if (list.status == 7 && list.order.unloadWeight != 0 && list.order.loadWeight != 0 && list.order.position == null){
      list.status = 6;//等待入库-->入库卸货
    } else if (list.status==1){
      list.status = 1;//待接单-->取消派单
    } else if (list.status == 3 && list.order.loadWeight != 0 && list.order.unloadWeight == 0) {
      list.status = 5;//已超时,运输中
    } else if (list.status == 3 && list.order.loadWeigh == 0) {
      list.status = 3;//已超时，待提货-->提货完成
    } else if (list.status == 3 && list.order.unloadWeight != 0 && list.order.loadWeight != 0 && list.order.position == null) {
      list.status = 6;//已超时，等待入库-->入库卸货
    } else if (list.status == 8) {
      list.status = 4;//已完成-->取消派单
    }
    return list
  },
  //计划时间
  bindSelect: function (e) {
    this.setData({
      stockunm: e.detail.value
    })
  },
  //实际库位
  factbindSelect: function (e) {
    this.setData({
      factstockunm: e.detail.value
    })
  },
  call: function (res) {
    wx.makePhoneCall({
      phoneNumber: res.currentTarget.data.num //仅为示例，并非真实的电话号码
    })
  },
  // 选择图片
  chooseImg: function (res) {
    var that = this;
    if(res.currentTarget.dataset.img=='zhuang'){
      that.setData({
        selectImg: []
      })
      var selectImg = that.data.selectImg;
      wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success: function (res) {
          var tempFilePaths = res.tempFilePaths
          selectImg.push(tempFilePaths[0])
          that.setData({
            selectImg: selectImg
          })
          that.uploadImg(tempFilePaths[0], "loadPicUrl");
        },
        fail: function () {
          that.tips('选取图片失败')
        }
      })
    }else{
      that.setData({
        select: []
      })
      var select = that.data.select;
      wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success: function (res) {
        var tempFilePaths = res.tempFilePaths
        select.push(tempFilePaths[0])
        that.setData({
          select: select
        })
        that.uploadImg(tempFilePaths[0], "unLoadPicUrl");
        },
        fail: function () {
          that.tips('选取图片失败')
        }
      })
    }
    
  },
  // 上传图片
  uploadImg: function (img,url) {
    var that = this;
    app.getLoginSession(function (session) {
      wx.uploadFile({
        url: app.api + '/wx_log_test/image/upload',
        filePath: img,
        name: 'file',
        formData: {
          file: img,
          token: session
        },
        method: 'POST',
        header: {
          'content-type': 'multipart/form-data'
        },
        success: function (res) {
          var rea = res.data;
          var data = JSON.parse(res.data);
          console.log(data);
          if (data.msg == "OK") {
            that.setData({
              [url]: data.data
            })
          } else {
            that.tips('图片上传失败')
          }
        },
        fail: function () {
          console.log("失败");
        }
      })
    })
  },
  //获取库位
  position: function (res) {
    var that = this;
    app.getLoginSession(function (session) {
      wx.request({
        url: app.api + '/wx_log_test/sys/position',
        data: {
          token: session
        },
        success: function (res) {
          that.setData({
            array:res.data.data
          })
        }
      })
    })

  },
  //获取用户输入
  input:function(res){
    var that = this;
    var value = res.detail.value;
    var type = res.currentTarget.dataset.type;
    if (type =='loadWeight'){
      that.setData({
        loadWeight:value
      })
    } else if (type == 'unloadWeight') {
      that.setData({
        unloadWeight: value
      })
      console.log(that.data.unloadWeight);
    }
  },
  //更新订单
  updateorder: function (res) {
    var that = this;
    if (that.data.data.status == 3 && !/^\d+(\.\d+)?$/.test(that.data.loadWeight)){
      wx.showModal({
        title: '提示',
        content: '请输入正确提货重量',
      })
    } else if (that.data.data.status == 5 && !/^\d+(\.\d+)?$/.test(that.data.unloadWeight)) {
      wx.showModal({
        title: '提示',
        content: '请输入正确卸货重量',
      })
      console.log(that.data.unloadWeight);
    } else if (that.data.data.status == 3 && that.data.loadPicUrl==undefined){
      wx.showModal({
        title: '提示',
        content: '请上传提货照片',
      })
    } else if (that.data.data.status == 5 && that.data.unLoadPicUrl==undefined) {
      wx.showModal({
        title: '提示',
        content: '请上传卸货照片',
      })
    }else if (that.data.data.status == 6 && that.data.factstockunm == undefined) {
      wx.showModal({
        title: '提示',
        content: '请选择实际入库位',
      })
    }else{
      app.getLoginSession(function (session) {
        wx.request({
          url: app.api + '/wx_log_test/order/update?token=' + session,
          data: {
            loadOn:that.data.data.order.loadOn,
            loadPicUrl: that.data.loadPicUrl == undefined ? that.data.data.order.loadWeight : that.data.loadPicUrl,
            loadWeight: that.data.loadWeight == undefined ? that.data.data.order.loadWeight : that.data.loadWeight,
            orderId: that.data.options.id,
            position: that.data.factstockunm == undefined ? that.data.data.order.position : that.data.array[that.data.factstockunm].value,
            prePosition: that.data.stockunm == undefined ? that.data.data.order.prePosition : that.data.array[that.data.stockunm].value,
            suspensionInfo: "string",
            unLoadPicUrl: that.data.unLoadPicUrl == undefined ? that.data.data.order.unLoadPicUrl : that.data.unLoadPicUrl,
            unloadOn: Date.parse(new Date(that.data.data.order.unloadOn)),
            unloadWeight: that.data.unloadWeight == undefined ? that.data.data.order.unloadWeight : that.data.unloadWeight,
            deliveryOn: Date.parse(new Date(that.data.data.order.deliveryOn)),
            goodWeight: that.data.data.order.goodWeight,
          },
          method:'POST',
          success:function(res){
            var res = res.data;
            if(res.msg=="OK"){
              wx.showToast({
                title: '操作成功！',
                icon:'success',
                success:function(res){
                  wx.navigateBack({
                    delta:1
                  })
                }
              })
            }else{
              wx.showToast({
                title: '请重试！',
                icon: 'loading',
              })
            }
          },
          fail:function(res){
            wx.showToast({
              title: '请重试！',
              icon: 'loading'
            })
          }
        })
      })
    }
    
  },
  //取消接单
  closeorder: function (res) {
    var that = this;
    app.getLoginSession(function (session) {
      wx.request({
        url: app.api + '/wx_log_test/order/unappoint?token=' + session + '&&orderid=' + res.currentTarget.dataset.orderid,
        method: 'POST',
        success: function (res) {
          var res = res.data;
          if (res.data == true) {
            wx.showModal({
              title: '温馨提示',
              content: '取消订单成功！',
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  wx.navigateBack({
                    delta:1
                  })
                }
              }
            })
          } else {
            wx.showToast({
              title: '失败，请重试！',
              icon: 'loading'
            })
          }
        }, fail: function (res) {
          wx.showToast({
            title: '失败，请重试！',
            icon: 'loading'
          })
        }
      })
    })
  }
})